package com.cloud.thelen.wallpaper.BaseUI;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.cloud.thelen.wallpaper.R;

public abstract class BaseFragment extends Fragment {

    private View rootView = null;
    protected BaseUI ui = null;
    protected View loadView = null;
    private FrameLayout contentView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_base, container, false);
            contentView = rootView.findViewById(R.id.base_content);
            loadView = inflater.inflate(getLayoutId(), null);
            contentView.addView(loadView);
            initViews(rootView);
        } else {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setEvent();
        getData();
    }

    protected void initViews(View view) {
        ui = new BaseUI(contentView);
    }

    protected abstract int getLayoutId();

    protected abstract void setEvent();

    protected abstract void getData();
}